// pages/screens/result/add/add.js
const util = require("../../../../utils/util")
const screencustomerservice = require("../../../../apiservice/screen/screencustomerservice");
const scResult = require('./getscresult.js')
const addbehavior = require('addbehavior')
const app = getApp();

const regeneratorRuntime = app.globalData.regeneratorRuntime;
Component({
	/**
	 * 页面的初始数据
	 */
	data: scResult.resultConfig,
	behaviors: [addbehavior],
	options: null,

	methods: {
		onCamera:function(){
			wx.navigateTo({
				url: "/pages/camera/camera"
			});
		 },
		//描述
		descChange: function (e) {
			this.setData({
				['descriptions.new_result']: e.detail
			})
		},
		//建议
		adviseChange: function (e) {
			this.setData({
				['advise.new_result']: e.detail
			})
		},
		//预约日期
		dateChange: function (e) {
			//console.log(e);
			this.setData({
				appointmenttothehospitaldate: e.detail.value,
				['reservation.new_reservationdate']: e.detail.value
			})
		},
		//眼科初步判断
		onChange: function (e) {
			console.log(e);
			if (e.currentTarget.id == "cataractRight") {
				var result = scResult.getCataractRes(e.detail.index);
				this.setData({
					['cataractRight.new_result']: result
				})
			} else if (e.currentTarget.id == "cataractLeft") {
				var result = scResult.getCataractRes(e.detail.index);
				this.setData({
					['cataractLeft.new_result']: result
				})
			} else if (e.currentTarget.id == "pterygiumRight") {
				var result = scResult.getPterygiumRes(e.detail.index);
				this.setData({
					['pterygiumRight.new_result']: result
				})
			} else if (e.currentTarget.id == "pterygiumLeft") {
				var result = scResult.getPterygiumRes(e.detail.index);
				this.setData({
					['pterygiumLeft.new_result']: result
				})
			} else if (e.currentTarget.id == "lacrimalObstruction") {
				var result = scResult.getRes(e.detail.index);
				this.setData({
					['lacrimalObstruction.new_result']: result
				})
			} else if (e.currentTarget.id == "trichiasis") {
				var result = scResult.getRes(e.detail.index);
				this.setData({
					['trichiasis.new_result']: result
				})
			} else if (e.currentTarget.id == "conjunctivitis") {
				var result = scResult.getRes(e.detail.index);
				this.setData({
					['conjunctivitis.new_result']: result
				})
			} else if (e.currentTarget.id == "ametropia") {
				var result = scResult.getRes(e.detail.index);
				this.setData({
					['ametropia.new_result']: result
				})
			} else if (e.currentTarget.id == "fundusDiseases") {
				var result = scResult.getRes(e.detail.index);
				this.setData({
					['fundusDiseases.new_result']: result
				})
			}
		},
		//姓名
		nameChange: function (e) {
			//console.log(e);
			var leadEntity = this.data.leadEntity;

			leadEntity.lastname = e.detail;

			this.setData({
				leadEntity: leadEntity
			})
		},
		onNameBlur: function (e) {
			let scid = this.data.screenId;
			var that = this;
			screencustomerservice.GetSameNameQuantity(scid, e.detail.value).then(function (res) {
				console.log(res.data);
				that.setData({
					IsShowtip: true,
					samenameqty: "+" + res.data
				})
			});
		},
		//性别
		genderChange: function (e) {
			//console.log(e);
			var leadEntity = this.data.leadEntity;
			var new_gender = 2;
			if (e.detail.index == 0) {
				new_gender = 1;
			}
			else {
				new_gender = 2;
			}
			this.setData({
				['reservation.new_gender']: new_gender,
				['propageanda.new_gender']: new_gender
			})

			leadEntity.new_gender = new_gender;
			this.setData({
				leadEntity: leadEntity
			})
		},
		//年龄
		ageChange: function (e) {
			if (!this.checkNum(e.detail)) {
				this.shouTips();
				this.setData({
					age: ""
				})
			} else {
				this.setData({
					['leadEntity.new_age']: e.detail,
					['reservation.new_age']: e.detail,
				})
			}
		},
		//医保类型
		insuranceTypeChange: function (e) {
			this.setData({
				insuranceTypeIndex: e.detail.value
			});
			// console.log(e);
			var leadEntity = this.data.leadEntity;
			if (e.detail.value != 0) {
				var new_insurancetype = this.data.insuranceType[e.detail.value].id;
				leadEntity.new_insurancetype = new_insurancetype;
				this.setData({
					['reservation.new_insurancetype']: new_insurancetype
				})
			}
			this.setData({
				leadEntity: leadEntity
			})
		},
		//筛查项目
		screenItemChange: function (e) {
			this.setData({
				screenitemIndex: e.detail.value
			});
			var leadEntity = this.data.leadEntity;
			if (e.detail.value != 0) {
				var new_screenitemId = this.data.screenitem[e.detail.value].new_screenitemId;
				leadEntity.new_screenitemId = new_screenitemId;
				this.setData({
					['leadEntity.new_screenitemId']: new_screenitemId,
					['sccustomer.new_scitemid']: new_screenitemId,
					['reservation.new_new_screenitem']: new_screenitemId,
				})
			}
			this.setData({
				leadEntity: leadEntity
			})
		},
		//手机号码
		phoneChange: function (e) {
			if (!this.checkNum(e.detail)) {
				this.shouTips();
				this.setData({
					phone: ''
				})
			} else {
				var leadEntity = this.data.leadEntity;
				if (e.detail.replace(/\s+/g, '') != '') {
					leadEntity.address1_telephone1 = e.detail;
					this.setData({
						['reservation.new_mobile']: e.detail,
						['propageanda.new_mobile']: e.detail,
					})
				}
				this.setData({
					leadEntity: leadEntity
				})
			}

		},
		//手机号码2
		phone1Change: function (e) {
			if (!this.checkNum(e.detail)) {
				this.shouTips();
				this.setData({
					phone2: ''
				})
			} else {
				var leadEntity = this.data.leadEntity;
				if (e.detail.replace(/\s+/g, '') != '') {
					leadEntity.new_mobile = e.detail;
				}
				this.setData({
					leadEntity: leadEntity
				})
			}
		},
		//固定电话
		telphoneChange: function (e) {
			if (!this.checkNum(e.detail)) {
				this.shouTips();
				this.setData({
					tel: ''
				})
			} else {
				var leadEntity = this.data.leadEntity;
				if (e.detail.replace(/\s+/g, '') != '') {
					leadEntity.telephone2 = e.detail;
					this.setData({
						['propageanda.new_tel']: e.detail
					})
				}
				this.setData({
					leadEntity: leadEntity
				})
			}
		},
		//固定电话2
		telphone1Change: function (e) {

			if (!this.checkNum(e.detail)) {
				this.shouTips();
				this.setData({
					tel2: ''
				})
			} else {
				var leadEntity = this.data.leadEntity;
				if (e.detail.replace(/\s+/g, '') != '') {
					leadEntity.address1_telephone2 = e.detail;
				}
				this.setData({
					leadEntity: leadEntity
				})
			}
		},
		//病种
		diseaseChange: function (e) {
			this.setData({
				diseaseIndex: e.detail.value
			});
			var leadEntity = this.data.leadEntity;
			var new_disease = this.data.diseaseArray[e.detail.value].id
			if (e.detail.value != 0) {
				leadEntity.new_disease = new_disease
				this.setData({
					['reservation.new_contnt']: new_disease
				})
			}
			this.setData({
				leadEntity: leadEntity
			})
		},
		//家庭住址
		addressChange: function (e) {
			var leadEntity = this.data.leadEntity;
			if (e.detail.replace(/\s+/g, '') != '') {
				leadEntity.address1_name = e.detail;
			}
			this.setData({
				leadEntity: leadEntity
			})
		},
		//身份证号
		citizenidChange: function (e) {
			var leadEntity = this.data.leadEntity;
			if (e.detail.replace(/\s+/g, '') != '') {
				leadEntity.new_citizenid = e.detail;
			}


			var idcard_patter =
				/^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
			var idnum = e.detail;
			if (idcard_patter.test(idnum)) {
				var born = idnum.substring(6, 10) + "-" + idnum.substring(10, 12) + "-" + idnum.substring(12, 14);
				var birthYear = parseInt(idnum.substring(6, 10));
				var currYear = new Date().getFullYear();
				var age = currYear - birthYear;
				leadEntity.new_age = age;
				leadEntity.new_birthdate = born;
				this.setData({
					age: age,

				})
			}
			this.setData({
				leadEntity: leadEntity,

			})

		},
		onIdCardBlur: function (e) {
			var idnum = e.detail.value;
			var sex = idnum.substring(idnum.length - 2, idnum.length - 1);
			var new_gender = 1;
			if (sex % 2 > 0) { //奇数
				new_gender = 1;
				this.setData({ ['idCardInfo.sex']: 0 });
			}
			else {
				new_gender = 2;
				this.setData({ ['idCardInfo.sex']: 1 });
			}
			this.setData({
				['reservation.new_gender']: new_gender,
				['propageanda.new_gender']: new_gender
			})
			var leadEntity = this.data.leadEntity;
			leadEntity.new_gender = new_gender;
			this.setData({
				leadEntity: leadEntity
			})
		},
		//社保卡号码
		insuranceidChange: function (e) {
			var leadEntity = this.data.leadEntity;
			if (e.detail.replace(/\s+/g, '') != '') {
				leadEntity.new_insuranceid = e.detail;
				this.setData({
					['reservation.new_insuranceid']: e.detail
				})
			}
			this.setData({
				leadEntity: leadEntity
			})
		},
		//右眼 手术情况
		nakedEyeRightChange: function (e) {

			if (e.detail.index == 0) {

				this.setData({
					['nakedEyeRightRes.new_result']: 'SC0000000095'
				})
			} else {
				this.setData({
					['nakedEyeRightRes.new_result']: 'SC0000000094'
				})
			}
			//console.log(this.data.nakedEyeRightRes);
		},
		//左眼 手术情况
		nakedEyeLeftChange: function (e) {
			if (e.detail.index == 0) {

				this.setData({
					['nakedEyeLeftRes.new_result']: 'SC0000000092'
				})
			} else {
				this.setData({
					['nakedEyeLeftRes.new_result']: 'SC0000000093'
				})
			}
			//console.log(this.data.nakedEyeLeftRes);
		},
		//选择结果
		onSelect: function (e) {
			//console.log(e);
			if (this.data.selectType == 1) {
				this.setData({
					nakedEyeRightText: e.currentTarget.dataset.name,
					nakedEyeRightId: e.currentTarget.id,
					['nakedEyeRight.new_result']: e.currentTarget.dataset.name
				});
			} else if (this.data.selectType == 2) {
				this.setData({
					nakedEyeLeftText: e.currentTarget.dataset.name,
					nakedEyeLeftId: e.currentTarget.id,
					['nakedEyeLeft.new_result']: e.currentTarget.dataset.name
				});
			} else if (this.data.selectType == 3) {
				this.setData({
					correctRightText: e.currentTarget.dataset.name,
					correctRightId: e.currentTarget.id,
					['correctRight.new_result']: e.currentTarget.dataset.name
				});
			} else if (this.data.selectType == 4) {
				this.setData({
					correctLeftText: e.currentTarget.dataset.name,
					correctLeftId: e.currentTarget.id,
					['correctLeft.new_result']: e.currentTarget.dataset.name
				});
			}
			this.setData({
				optionShow: false
			});
		},
		//矫正视力 右眼
		nakedEyeRight: function () {
			this.setData({
				optionShow: true,
				selectType: 1
			});
		},
		//矫正视力 左眼
		nakedEyeLeft: function () {
			this.setData({
				optionShow: true,
				selectType: 2
			});
		},
		//是否跟进
		followUpChange: function (e) {
			//console.log(e);
			if (e.detail.index == 0) {
				this.setData({
					['propageanda.new_isfollowself']: 2
				})
			} else {
				this.setData({
					['propageanda.new_isfollowself']: 1
				})
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function (options) {
			this.setData({ options: options });
			console.log(this.data.startReservationDate, this.data.endReservationDate);
			if (options.screenitemId) {
				this.getSchoolListByType(options.screenitemId);
			}

			this.GetAllHospital();
			this.initData(options);
			this.setData({
				screenitemId: options.screenitemId,
				screentype: options.screentype
			})
			if (options.stage) {
				this.setData({
					stage: options.stage
				})
			}

		},
		onClose: function () {
			this.setData({
				optionShow: false
			})
		},
		//全身疾病史
		checkboxonChange: function (e) {
			//console.log(e);
			this.setData({
				result: e.detail
			})
			//console.log(this.data.result);

		},
		//是否预约
		reservationonChange: function (e) {
			if (e.detail.index == 0) {
				let userinfo = app.globalData.userInfo;
				var hospitalindex = this.getPickerValue(this.data.hospitalList, 'siteId', userinfo.siteid);
				var siteId = this.data.hospitalList[hospitalindex].siteId;
				this.setData({
					reservationShow: true,
					hospitalindex: hospitalindex,
					['reservation.new_hospital']: siteId,
					['reservation.new_reservationdate']: this.data.appointmenttothehospitaldate,
					['reservation.new_contnt']: this.data.itemData[this.data.itemindex].id,
					iSreservation: true,
					['propageanda.new_isorder']: true,
					['leadEntity.new_reservation']: true
				});
				this.GetDepartmentsByHospID(siteId);
			} else {
				this.setData({
					reservationShow: false,
					iSreservation: false,
					['propageanda.new_isorder']: false,
					['leadEntity.new_reservation']: false
				})
			}
		},
		//矫正视力 右眼
		correctRight: function () {
			this.setData({
				optionShow: true,
				selectType: 3
			})
		},
		//矫正视力 左眼
		correctLeft: function () {
			this.setData({
				optionShow: true,
				selectType: 4
			})
		},
		//清空按钮
		onClear: function () {
			if (this.data.selectType == 1) {
				this.setData({
					nakedEyeRightText: '请填写',
					nakedEyeRightId: 0
				});
			} else if (this.data.selectType == 2) {
				this.setData({
					nakedEyeLeftText: '请填写',
					nakedEyeLeftId: 0
				});
			} else if (this.data.selectType == 3) {
				this.setData({
					correctRightText: '请选择',
					correctRightId: 0
				});
			} else if (this.data.selectType == 4) {
				this.setData({
					correctLeftText: '请选择',
					correctLeftId: 0
				});
			}
			this.setData({
				optionShow: false
			});
		},
		changeHospital: function (e) {
			var siteId = this.data.hospitalList[e.detail.value].siteId;
			this.setData({
				hospitalindex: e.detail.value,
				['reservation.new_hospital']: siteId
			});
			this.GetDepartmentsByHospID(siteId);
			if (this.data.officeArray.length > 0) {
				//console.log(this.data.officeArray);
				var officeIndex = this.data.officeIndex;
				var new_department = this.data.officeArray[officeIndex].new_departmentId;
				this.setData({
					['reservation.new_department']: new_department
				});
			}
		},
		//科室
		bindOfficeChange: function (e) {
			this.setData({
				officeIndex: e.detail.value,
				['reservation.new_department']: this.data.officeArray[e.detail.value].new_departmentId
			});
		},
		//项目
		bindItemChange: function (e) {
			this.setData({
				itemindex: e.detail.value,
				['reservation.new_contnt']: this.data.itemData[e.detail.value].id
			})
		},
		//药品数量
		medicineChange: function (e) {
			this.setData({
				['propageanda.new_countteardrop']: e.detail
			})
		},
		//备注
		remarkChange: function (e) {
			this.setData({
				['leadEntity.description']: e.detail,
				['reservation.new_remark']: e.detail
			})
		},
		//打开建议
		onClickIcon: function (e) {
			this.setData({
				adviseshow: true
			})
			//console.log(e);
		},
		//关闭建议
		adviseonClose: function () {
			this.setData({
				adviseshow: false
			})
		},
		//选择建议
		adviseonSelect: function (e) {
			//console.log(e);
			this.setData({
				adviseshow: false,
				advisevalue: e.detail.name,
				['advise.new_result']: e.detail.name
			})
		},

		//扫描身份证
		scanSuccess: function (e) {
			debugger;
			console.log(e.detail);
			//var age = util.getAgeBybirthDay(e.detail.birthday);
			var idnum = e.detail.id.text;
			var birthYear = parseInt(idnum.substring(6, 10));
			var currYear = new Date().getFullYear();
			let age = currYear - birthYear;
			var new_gender = (e.detail.gender.text == "女" ? 1 : 0) + 1;

			var leadEntity = this.data.leadEntity;
			leadEntity.lastname = e.detail.name.text;
			leadEntity.new_gender = new_gender;
			leadEntity.new_age = age;
			leadEntity.address1_name = e.detail.address.text;
			leadEntity.new_citizenid = idnum;
			this.setData({
				idCardInfo: e.detail,
				age: age ? age : "",
				leadEntity: leadEntity,
				['reservation.new_gender']: new_gender,
				['propageanda.new_gender']: new_gender
			})

		},

		setPersonIdCard:function(idCard){
			
			let { birthDate, gender, address, nationality, name, idNumber } = idCard;

			let idCardInfo = {
				name: { text: name },
				gender: { text: gender },
				address: { text: address },
				nationality: { text: nationality },
				id: { text: idNumber },
				sex: gender
			};

			debugger;
			let birthYear = parseInt(birthDate.substring(0, 4));
			let currYear = new Date().getFullYear();
			let age = currYear - birthYear;
			let new_gender = (gender == "女" ? 1 : 0) + 1;

			let leadEntity = this.data.leadEntity;
			leadEntity.lastname = name;
			leadEntity.new_gender = new_gender;
			leadEntity.new_age = age;
			leadEntity.address1_name = address;
			leadEntity.new_citizenid = idNumber;
			this.setData({
				idCardInfo: idCardInfo,
				age: age ? age : "",
				leadEntity: leadEntity,
				['reservation.new_gender']: new_gender,
				['propageanda.new_gender']: new_gender
			})
			
		},

		//提交按钮
		addResult: async function () {
			var CreateResults = this.getALLscResult();
			//this.data.setData({leadEntity:})
			var lead = this.data.leadEntity;
			if (this.data.screentype == '100000000') { //社区
				lead.new_patientsource = 3;
			} else if (this.data.screentype == '100000001') { //中小学校园
				lead.new_patientsource = 7;
			} else if (this.data.screentype == '100000002') { //大学校园
				lead.new_patientsource = 8;
			} else if (this.data.screentype == '100000003') { //大客户
				lead.new_patientsource = 6;
			}
			var reservation = this.data.reservation;
			var propageanda = this.data.propageanda;
			var customer = this.data.sccustomer;
			var scobj = {};
			//scobj.new_screenid=this.data.new_screenid;
			scobj.lead = lead;
			if (this.data.iSreservation) {
				scobj.reservation = reservation;
			}
			scobj.propaganda = propageanda;
			scobj.customer = customer;
			scobj.CreateResults = CreateResults;
			var mobilereg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
			var idreg =
				/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/

			//var a = this.data.idCardInfo.name;
			if (lead.lastname.replace(/\s+/g, '') == '') {
				wx.showToast({
					title: '请输入姓名',
					icon: 'none',
					duration: 1500
				});
				return false;
			}

			if (!lead.new_disease) {
				wx.showToast({
					title: '请选择病种',
					icon: 'none',
					duration: 1500
				})
				return false;
			}

			if (lead.new_age != '') {
				if (lead.new_age > 150) {
					wx.showToast({
						title: '请填写正确的年龄',
						icon: 'none',
						duration: 1500
					})
					return false;
				}
			}
			//idCardInfo.id
			if (this.data.leadEntity.address1_telephone1) {
				if (!mobilereg.test(this.data.leadEntity.address1_telephone1)) {
					wx.showToast({
						title: '请填写正确的手机号码',
						icon: 'none',
						duration: 1500
					})
					return false;
				}
			}

			if (this.data.leadEntity.new_mobile) {
				if (!mobilereg.test(this.data.leadEntity.new_mobile)) {
					wx.showToast({
						title: '请填写正确的手机号码2',
						icon: 'none',
						duration: 1500
					})
					return false;
				}
			}
			if (this.data.leadEntity.new_citizenid) {
				if (!idreg.test(this.data.leadEntity.new_citizenid)) {
					wx.showToast({
						title: '请填写正确的身份证号码',
						icon: 'none',
						duration: 1500
					})
					return false;
				}
			}

			if (this.data.leadEntity.new_reservation) {
				if (this.data.itemindex == 0) {
					wx.showToast({
						title: '项目不能为空!',
						duration: 1500,
						icon: 'none'
					})
					return false;
				}

			}

			wx.showLoading({
				title: '提交中',
				mask: true
			})

			let gender = this.data.leadEntity.new_gender;
			await this.CreateSQSC(scobj);
			console.log(scobj);

			this.data.leadEntity = scResult.resultConfig.leadEntity;
			this.setData({
				['leadEntity.new_gender']: gender,
				['idCardInfo.sex']: (gender == 1 ? "男" : "女"),
				['leadEntity.telephone2']: "",
				['leadEntity.address1_telephone2']: ""
			})
			this.initData(this.data.options);// = scResult.resultConfig;
		}
	}
})
